using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Events;

/// <summary>
/// 场景切换模块
/// </summary>
public class ScenesMgr : Singleton<ScenesMgr>
{
    /// <summary>
    /// 同步切换场景
    /// </summary>
    /// <param name="name"></param>
    /// <param name="fun"></param>
    public void LoadScene(string name,UnityAction fun)
    {
        //加载场景
        SceneManager.LoadScene(name);
        //加载完成后 才会去执行fun
        fun();
    }
    /// <summary>
    /// 异步协程加载场景
    /// </summary>
    /// <param name="name"></param>
    /// <param name="fun"></param>
    public void LoadSceneAsyn(string name,UnityAction fun)
    {
        MonoMgr.Instance().StartCoroutine(ReallyLoadSceneAsyn(name, fun));
    }

    private IEnumerator ReallyLoadSceneAsyn(string name,UnityAction fun)
    {
        float disProgress = 0;
        float currentProgress = 0;

        AsyncOperation ao = SceneManager.LoadSceneAsync(name);
        ao.allowSceneActivation = false;

        while (currentProgress<0.9f)
        {
            currentProgress = ao.progress;
            while (disProgress < currentProgress)
            {
                disProgress += 0.01f;
                EventCenter.Instance().EventTrigger("场景切换进度条更新", disProgress);
            }
            yield return currentProgress;
        }

        while (disProgress<=1)
        {
            disProgress += 0.01f;
            EventCenter.Instance().EventTrigger("场景切换进度条更新", disProgress);
            yield return disProgress;
        }
        //ao.allowSceneActivation = true;
        //可以得到场景加载的一个进度
        while (!ao.isDone)
        {
            //事件中心 向外分发 进度情况  外面想用就用
            EventCenter.Instance().EventTrigger("场景切换进度条更新", 1f);
            if (disProgress >= 0.9f)
            {
                ao.allowSceneActivation = true;

            }
            //这里面去更新进度条
            yield return ao.progress;
        }
        //加载完成过后 才会去执行fun
        fun();
    }
}
